package com.lili.order.service.impl.complaint;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lili.model.order.entity.PageMap;
import com.lili.model.order.entity.aftersale.AfterSaleReason;
import com.lili.model.order.entity.complaint.OrderComplaint;
import com.lili.model.order.entity.sqlException;
import com.lili.order.mapper.aftersale.AfterSaleReasonMapper;
import com.lili.order.mapper.complaint.OrderComplaintMapper;
import com.lili.order.service.complaint.OrderComplaintService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Map;

@Service
public class OrderComplaintServiceImpl implements OrderComplaintService {
    @Autowired
    OrderComplaintMapper orderComplaintMapper;
    @Autowired
    AfterSaleReasonMapper afterSaleReasonMapper;
    @Override
    public int save(OrderComplaint orderComplaint) {
        int rs=orderComplaintMapper.insert(orderComplaint);
        if(rs==0){
            return  rs;
        }
        AfterSaleReason afterSaleReason=getReason(orderComplaint);
        rs=afterSaleReasonMapper.insert(afterSaleReason);
        if(rs==0){
            putThrow("投诉原因添加失败！");
        }
           return rs;
    }

    @Override
    public int delete(Serializable id) {
        return orderComplaintMapper.myDeleteById(id);
    }

    @Override
    public int update(OrderComplaint orderComplaint) {
        QueryWrapper<OrderComplaint> wrapper=new QueryWrapper<>();
        wrapper.eq("id",orderComplaint.getId());
        int rs=  orderComplaintMapper.update(orderComplaint,wrapper);
        if(rs==0){
            return 0;
        }
        if(orderComplaint.getContent()!=null){
             AfterSaleReason afterSaleReason =new AfterSaleReason();
             afterSaleReason.setReason(orderComplaint.getContent());
             afterSaleReason.setReasonId(orderComplaint.getId());
             rs=afterSaleReasonMapper.updateReason(afterSaleReason);
            if(rs==0){
                System.out.println("rs=0有可能售后原因已经被删除");
            }
        }
        return 1;
    }

    @Override
    public OrderComplaint queryById(Serializable id) {
        return orderComplaintMapper.selectById(id);
    }

    @Override
    public  Page<OrderComplaint>  queryByPage(Map map) {
        QueryWrapper<OrderComplaint> wrapper=new QueryWrapper<>();
        if(map.containsKey("orderSn")){
            wrapper.like("order_sn",map.get("orderSn"));
        }
        if(map.containsKey("memberName")){
            wrapper.like("member_id",map.get("memberName"));
        }
        if(map.containsKey("complainStatus")){
            wrapper.eq("complain_status",map.get("complainStatus"));
        }
        if(map.containsKey("storeId")){
            wrapper.like("store_id",map.get("storeId"));
        }
        PageMap pageMap=new PageMap(map);
        Page<OrderComplaint> page=new Page<>(pageMap.getPage(),pageMap.getPageSize());
        orderComplaintMapper.selectPage(page,wrapper);
        return page;

    }
    public AfterSaleReason getReason(OrderComplaint orderComplaint){
        AfterSaleReason afterSaleReason=new AfterSaleReason();
        afterSaleReason.setReasonId(orderComplaint.getId());
        afterSaleReason.setReason(orderComplaint.getContent());
        afterSaleReason.setServiceType("投诉");
        afterSaleReason.setCreateTime(orderComplaint.getCreateTime());
        return afterSaleReason;
    }
    protected void putThrow(String message) {
        try {
            throw new sqlException(message);
        } catch (sqlException e) {
            throw new RuntimeException(e);
        }
    }
}
